//index.js
//获取应用实例
var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({
    data: {
        imgurl: [],
        category: [],
        goods:[],
        img_url: c.img_url,
        keyword:""
    },
    onLoad: function (options) {
        this.getCat();
        this.getSlide();
        this.getGoods();
    },
    onPullDownRefresh: function () {
        this.getCat();
        this.getSlide();
        this.getGoods();
    },
    getCat: function () {
        var that = this;
        var data = {
            url: "/comv1/nav/lists",
            cat_id: 2,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    data[index].pic = c.img_url + data[index].pic;
                }
                that.setData({
                    category: data
                });
            } else {

            }
        });
    }, 
    getSlide: function () {
        var that = this;
        var data = {
            url: "/comv1/slide/lists",
            cat_id: 1,
            page:1,
            limit:4,
            keyword:""
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    data[index].pic = c.img_url + data[index].pic;
                }
                that.setData({
                    imgurl: data
                });
            } else {

            }
        });
    },
    getGoods:function(){
        var that = this;
        var data = {
            url: "/v1/goods/floor",
            rec_ids:"1,2,3,4,5,6"
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                that.setData({
                    goods: data
                });
            } else {

            }
        });
    },
    editKeyword:function(e){
        this.setData({
            keyword: e.detail.value
        })
    },
    searchGoods:function(){
        var keyword = this.data.keyword;
        wx.navigateTo({
            url: '/pages/shop/goodslist/index?keyword='+keyword,
        })
    },
    //分类图片错误处理
    errCategoryPic:function(e){
      var index = e.target.dataset.index;
      var category = this.data.category;
      category[index]['pic'] = "/static/images/default.png";
      this.setData({
        category: category
      });
    },
    //幻灯图片错误处理
    errimgurlPic: function (e) {
      var index = e.target.dataset.index;
      var imgurl = this.data.imgurl;
      imgurl[index]['pic'] = "/static/images/default.png";
      this.setData({
        imgurl: imgurl
      });
    },
    //分享
    onShareAppMessage: function (res) {
      var that = this;
      if (res.from === "button") {
        var id = res.target.dataset.id;
        var name = res.target.dataset.name;
        var pic = res.target.dataset.pic;
        var description = res.target.dataset.description;
        var price = res.target.dataset.price;
        return {
          title: name,
          path: '/pages/shop/goodsdetail/index?id=' + id + "&s_openid=" + wx.getStorageSync("openId"),
          imageUrl: pic,
          success: function (res) {
            wx.getSystemInfo({
              success: function (d) {
                var token = wx.getStorageSync("userinfo").wap_token;
                var data = {
                  url: "/v1/usershare/add",
                  token: token,
                  type: 0,
                  id: id
                };
                utils.request(data).then(function (res) {
                  if (res.code == 0) {

                  }
                });
              }
            })
          },
        }
      }
    },
    jump: function (res) {
      utils.r("/pages/shop/goodsdetail/index?id=" + res.currentTarget.dataset.id);
    },

})
